package com.sinosoft.ebusiness.controller;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.RequestBody;
//import ins.framework.common.Page;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.sinosoft.ebusiness.domain.elementManage.GeTemplate;
import com.sinosoft.ebusiness.service.TemplateService;
import com.sinosoft.ebusiness.service.repo.GeTemplateRepository;
@RestController
//@Controller
@RequestMapping("/template/")
public class GeTemplateController {
	
	@Autowired
	private GeTemplateRepository geTemplateRepository;	
	
	@Autowired
	private TemplateService templateService;
	
	@RequestMapping(value="queryTemplateByPk.action",method=RequestMethod.GET)
	public GeTemplate findTemplate(String templateCode) throws IOException {
//		String templatecode = "xxxxxx1";
		GeTemplate geTemplate = geTemplateRepository.findByTemplatecode(templateCode);
		/*ObjectMapper mapper = new ObjectMapper();
		String str = mapper.writeValueAsString(geTemplate);
		
		geTemplate = JSON.parseObject(str, GeTemplate.class);*/
//		geTemplate = mapper.readValue(str, GeTemplate.class);
		
		return geTemplate;
	}
	
	@RequestMapping(value="queryTemplate.action",method=RequestMethod.POST)
	public Page findTempaltePage(@RequestBody GeTemplate template,int pageNo,int pageSize,String flag) {
//		int pageNo = 1;
//		int pageSize = 10;
//		GeTemplate template = new GeTemplate();
		Map<String,Object> map = new HashMap();
		Page page = null;
		page = templateService.queryGeTemplateList(template, pageNo, pageSize,flag);
		/*map.put("page",page.getContent());  
		map.put("totalPage",page.getTotalElements()); 
		map.put("pageNo", page.getNumber());
		map.put("pageSize", page.getSize());
		map.put("totalCount", page.getTotalElements());*/
		return page;
	}
	
	@RequestMapping(value="findJpaTempaltePage.action",method=RequestMethod.POST)
	public Page findJpaTempaltePage(int pageNo,int pageSize,String flag) throws JsonProcessingException {
		GeTemplate template = new GeTemplate();
		Page page = null;
		
//		Sort sort = new Sort(Direction.DESC, "id");
//	    Pageable pageable = new PageRequest(pageNo, pageSize, sort);
	    
		page = this.templateService.queryGeTemplateList(template, pageNo, pageSize,flag);
		
//		ObjectMapper mapper = new ObjectMapper();
//		String str = mapper.writeValueAsString(page);
		
		
		/*page = this.geTemplateRepository.findPage(new Specification<GeTemplate>() {

			@Override
			public Predicate toPredicate(Root<GeTemplate> root, CriteriaQuery<?> query, CriteriaBuilder cb) {
				if(template.getTemplatecode()!=null&&!"".equals(template.getTemplatecode().trim())){
					query.where(cb.like(root.<String>get("templatecode"), "%"+template.getTemplatecode().trim()+"%"));
				}
				if(template.getTemplatecname()!=null&&!"".equals(template.getTemplatecname().trim())){
					query.where(cb.like(root.<String>get("templatecname"), "%"+template.getTemplatecname().trim()+"%"));
				}
				if(template.getTemplateename()!=null&&!"".equals(template.getTemplateename().trim())){
					query.where(cb.like(root.<String>get("templateename"), "%"+template.getTemplateename().trim()+"%"));
				}
				if(template.getTemplatetname()!=null&&!"".equals(template.getTemplatetname().trim())){
					query.where(cb.like(root.<String>get("templatetname"), "%"+template.getTemplatetname().trim()+"%"));
				}
				if(template.getRiskClass()!=null&&!"".equals(template.getRiskClass().trim())){
					query.where(cb.like(root.<String>get("riskClass"), "%"+template.getRiskClass().trim()+"%"));
				}
				return null;
			}
			
		},new PageRequest(pageNo, pageSize));*/
		return page;
	}
	
	@RequestMapping(value="goIndex.action",method=RequestMethod.GET)
	public String goIndex() {
		return "index";
	}
	
	@RequestMapping(value="goTemplate.action",method=RequestMethod.GET)
	public String goTemplate() {
		return "template";
	}
}
